<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$tables = array();
$allTables = $DB->fetchAll("SHOW TABLES");

foreach($allTables as $k=>$v){
    foreach($v as $key=>$tablename){
        $tables[] = $tablename;
    }
}



if(Antz_IntelliForm::submitted('jfxbup-settings')){
    $cronMin = preg_replace('/[^0-9\*]/', '', post('cronMin'));
    $cronHour = preg_replace('/[^0-9\*]/', '', post('cronHour'));
    $cronDayMonth = preg_replace('/[^0-9\*]/', '', post('cronDayMonth'));
    $cronMonth = preg_replace('/[^0-9\*]/', '', post('cronMonth'));
    $cronDayWeek = preg_replace('/[^0-9\*]/', '', post('cronDayWeek'));
    $path = post('path', false, 255);
    if($path === false) $path = '';
    $os = post('os');

    if(strlen($path)>0) $path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '/').'/';

    if($cronMin == '') $cronMin = '0';
    if($cronHour == '') $cronHour = '3';
    if($cronDayMonth == '') $cronDayMonth = '*';
    if($cronMonth == '') $cronMonth = '*';
    if($cronDayWeek == '') $cronDayWeek = '*';

    $postTables = post('tables');
    $newTables = array();

    foreach($postTables as $k=>$tablename){
        if(in_array($tablename, $tables)){
            $newTables[] = $tablename;
        }
    }

    $dbParams = array(
        'v' => implode(',', $newTables)
    );
    $DB->update($CONFIG->dbprefix.'jfxbup_config', $dbParams, "k = 'tables'");
    $dbParams = array(
        'v' => $cronMin.','.$cronHour.','.$cronDayMonth.','.$cronMonth.','.$cronDayWeek
    );
    $DB->update($CONFIG->dbprefix.'jfxbup_config', $dbParams, "k = 'cron'");
    $DB->update($CONFIG->dbprefix.'jfxbup_config', array('v' => $os), "k = 'operating_system'");
    $DB->update($CONFIG->dbprefix.'jfxbup_config', array('v' => $path), "k = 'path_to_mysqldump'");


    $thisMin = (int) date('i');
    $thisHour = date('H');
    $thisDayMonth = date('j');
    $thisMonth = date('n');
    $thisDayWeek = date('w');

    $dbParams = array(
        'v' => $thisMin.','.$thisHour.','.$thisDayMonth.','.$thisMonth.','.$thisDayWeek
    );
    $DB->update($CONFIG->dbprefix.'jfxbup_config', $dbParams, "k = 'last_run'");

}







$selectedTables = $DB->oneValue($CONFIG->dbprefix.'jfxbup_config', 'v', "k = 'tables'");
$selectedTables = explode(',', $selectedTables);


$tableSelect = '<select name="tables[]" multiple="multiple" size="'.count($tables).'">';
foreach($tables as $k=>$tablename){
    if(in_array($tablename, $selectedTables)) $selected = 'selected="selected"';
    else $selected = '';
    $tableSelect .= '<option value="'.$tablename.'" '.$selected.'>'.$tablename.'</option>';
}
$tableSelect .= '</select>';

$os = $this->getConfig('operating_system');
$path = $this->getConfig('path_to_mysqldump');

$cron = explode(',', $DB->oneValue($CONFIG->dbprefix.'jfxbup_config', 'v', "k = 'cron'"));

$cronMin = '<input type="text" name="cronMin" value="'.$cron[0].'" />';
$cronHour = '<input type="text" name="cronHour" value="'.$cron[1].'" />';
$cronDayMonth = '<input type="text" name="cronDayMonth" value="'.$cron[2].'" />';
$cronMonth = '<input type="text" name="cronMonth" value="'.$cron[3].'" />';
$cronDayWeek = '<input type="text" name="cronDayWeek" value="'.$cron[4].'" />';

?>
<form method="post">
<?php echo Antz_IntelliForm::seed('jfxbup-settings'); ?>
<table id="jfxbup-settings">
    <tbody>
        <tr>
            <th>Minute</th>
            <th>Hour</th>
            <th>Day Of Month</th>
            <th>Month</th>
            <th>Day Of Week</th>
        </tr>
        <tr>
            <td><?php echo $cronMin; ?></td>
            <td><?php echo $cronHour; ?></td>
            <td><?php echo $cronDayMonth; ?></td>
            <td><?php echo $cronMonth; ?></td>
            <td><?php echo $cronDayWeek; ?></td>
        </tr>
    </tbody>
</table>

<?php echo $tableSelect; 

$input = new JFX_Form_Components_Input('text', 'path', 'Path to mysqldump', post('path', $path));
echo (string) $input;

$sel = new JFX_Form_Components_Select('os', 'Operating System', post('os', $os));
$sel->add('Unknown', 'unknown');
$sel->add('Unix/Linux', 'unix');
$sel->add('Windows', 'windows');

echo (string) $sel;

?>


<div align="center" style="clear: both"><input type="submit" value="Update Settings" /></div>

</form>



